﻿Imports System.Data.OleDb
Imports LecturerAppointmentSystem.UserDetails
Imports LecturerAppointmentSystem.AppointmentDetails
Imports System.Data.SqlClient

Public Class MonthView
    Inherits System.Web.UI.Page

    'Dim appointmentDates As Array
    'Dim bookedBy As Array

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AppointmentCalendar.VisibleDate = Request.QueryString("Date")
        'appointmentDates = getAppointmentDates()
    End Sub

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles AppointmentCalendar.SelectionChanged
        Response.Redirect("Calendar.aspx?Date=" & CType(AppointmentCalendar.SelectedDate, Date) & "&UserId=" & getUserId(User.Identity.Name))
    End Sub

    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs) Handles AppointmentCalendar.DayRender
        Dim appointmentDetails As DataSet = getAppointments()
        Dim onmouseoverStyle As String = "this.style.backgroundColor='#D4EDFF'"
        Dim onmouseoutStyle As String = "this.style.backgroundColor='@BackColor'"
        Dim rowBackColor As String = String.Empty
        Dim nextDate As DateTime
        Dim booked As Boolean
        Dim bookedBy As String
        Dim link As HyperLink
        Dim unTouched As Boolean = True
        Try

            If Not appointmentDetails Is Nothing Then
                For Each dataRow In appointmentDetails.Tables(0).Rows
                    nextDate = CType(dataRow("AppointmentDate"), Date)
                    booked = CType(dataRow("Booked"), Boolean)
                    If nextDate = e.Day.Date Then
                        link = New HyperLink()
                        If booked Then
                            bookedBy = dataRow("Student_FirstName") & " " & dataRow("Student_LastName")
                            link.Text = "Appointment with " & bookedBy
                        Else
                            link.Text = "Unbooked Appointment"
                        End If

                        link.NavigateUrl = "AppointmentDetails.aspx?ID=" & dataRow("ID")
                        link.BackColor = Drawing.Color.BlanchedAlmond
                        link.ToolTip = "Click to get more detailed information."
                        link.Font.Name = "Arial Narrow"
                        link.Font.Bold = False
                        link.ForeColor = System.Drawing.ColorTranslator.FromHtml("Blue")
                        e.Cell.BackColor = Drawing.Color.Beige
                        e.Cell.Controls.Add(New LiteralControl("<br />"))
                        e.Cell.Controls.Add(link)
                        unTouched = False
                    End If
                Next
            End If
        Catch ex As Exception

        End Try

        If unTouched AndAlso Not e.Day.IsWeekend AndAlso Not e.Day.IsToday Then
            e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
            e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))
        End If

    End Sub

    Protected Sub Calendar1_Month(ByVal sender As Object, ByVal e As MonthChangedEventArgs) Handles AppointmentCalendar.VisibleMonthChanged
        Dim year As Integer = e.NewDate.Year
        Dim month As Integer = e.NewDate.Month
        Response.Redirect("MonthView.aspx?Date=1/" & month.ToString() & "/" & year.ToString() & "&UserId=" & getUserId(User.Identity.Name))
        'AppointmentCalendar.SelectedDate = New DateTime(e.NewDate.Year, e.NewDate.Month, 1)
        'AppointmentCalendar.VisibleDate = New DateTime(e.NewDate.Year, e.NewDate.Month, 1)
        AppointmentCalendar.DataBind()
    End Sub

    'Function getAppointmentDates()
    '    Dim appointmentDates As New List(Of Date)
    '    Dim sql As String = "SELECT AppointmentDate FROM AppointmentsWithLecturerAndStudentDetails WHERE Lecturer_Fkid_UserId = @Lecturer_Fkid_UserId"
    '    Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
    '    cmd.Parameters.AddWithValue("@Lecturer_Fkid_UserId", Request.QueryString("UserId"))
    '    If conn.State = ConnectionState.Closed Then
    '        conn.Open()
    '    End If
    '    Dim reader As OleDbDataReader = cmd.ExecuteReader()
    '    While (reader.Read())
    '        appointmentDates.Add(CType(reader("AppointmentDate"), Date))
    '    End While
    '    conn.Close()
    '    Return appointmentDates.ToArray()
    'End Function

    'Function getBookedBy(ByVal appointmentDate As Date)
    '    Dim appointmentId As Integer
    '    Dim bookedBy As New List(Of String)
    '    Dim sql As String = "SELECT Student_FirstName, Student_LastName FROM AppointmentsWithLecturerAndStudentDetails WHERE Lecturer_Fkid_UserId = @Lecturer_Fkid_UserId AND AppointmntDate = @AppointmentDate"
    '    Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
    '    cmd.Parameters.AddWithValue("@Lecturer_Fkid_UserId", Request.QueryString("UserId"))
    '    cmd.Parameters.AddWithValue("@AppointmentDate", appointmentDate)
    '    conn.Open()
    '    Dim reader As OleDbDataReader = cmd.ExecuteReader()
    '    While (reader.Read())
    '        bookedBy.Add(reader("Student_Firstname") & " " & reader("Student_LastName"))
    '    End While
    '    conn.Close()
    '    Return bookedBy.ToArray()
    'End Function

    Function getAppointments()
        Dim ds As DataSet = New DataSet()
        Dim schemaTable As DataTable
        Dim dataTable As DataTable = New DataTable()
        Try
            Dim sql As String = "SELECT * FROM AppointmentsWithLecturerAndStudentDetails WHERE Lecturer_Fkid_UserId = @Lecturer_Fkid_UserId"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@Lecturer_Fkid_UserId", Request.QueryString("UserId"))
            conn.Open()
            Dim reader As OleDbDataReader = cmd.ExecuteReader()
            schemaTable = reader.GetSchemaTable()
            Dim intCounter As Integer

            For intCounter = 0 To schemaTable.Rows.Count - 1
                Dim dataRow As DataRow = schemaTable.Rows(intCounter)
                Dim columnName As String = CType(dataRow("ColumnName"), String)
                Dim column As DataColumn = New DataColumn(columnName, CType(dataRow("DataType"), Type))
                dataTable.Columns.Add(column)
            Next
            ds.Tables.Add(dataTable)
            While reader.Read()
                Dim dataRow As DataRow = dataTable.NewRow()
                For intCounter = 0 To reader.FieldCount - 1
                    dataRow(intCounter) = reader.GetValue(intCounter)
                Next
                dataTable.Rows.Add(dataRow)
            End While
        Catch ex As Exception
            Response.Write(ex.ToString())
        Finally
            conn.Close()
        End Try
        Return ds
    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_dayView.Click
        Dim dt = Request.QueryString("Date")
        Response.Redirect("Calendar.aspx?Date=" & dt & "&UserId=" & getUserId(User.Identity.Name))
    End Sub
End Class